package org.dynamicallyacquired.jwt;

import cn.soboys.restapispringbootstarter.Assert;
import cn.soboys.restapispringbootstarter.HttpStatus;
import cn.soboys.restapispringbootstarter.authorization.LoginAuthorization;
import cn.soboys.restapispringbootstarter.authorization.UserJwtToken;
import org.dromara.hutool.core.text.StrUtil;
import org.dynamicallyacquired.domain.AndroidInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyLoginAuthorization implements LoginAuthorization {
@Resource
private UserJwtToken userJwtToken;

@Override
public Boolean authorization(HttpServletRequest request, HttpServletResponse response, Object handler) {
    String token = request.getHeader("Token");

    Assert.isFalse(StrUtil.isEmpty(token),HttpStatus.UNAUTHORIZED);
    String userId = userJwtToken.getUserId(token);
    AndroidInfo androidInfo = userJwtToken.parseUserToken(userId, AndroidInfo.class);
    return androidInfo != null;

    }
}